<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model.trim="form.name" placeholder="请输入角色名称" />
      </el-form-item>
      <el-form-item label="代码" prop="code">
        <el-input v-model.trim="form.code" placeholder="请输入代码" />
      </el-form-item>
      <el-form-item label="描述" prop="roleDesc">
        <el-input v-model.trim="form.roleDesc" placeholder="请输入描述" />
      </el-form-item>
      <el-form-item label="启用状态" prop="isEnable">
        <el-radio-group v-model="form.isEnable">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">停用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="数据范围" prop="dataScope">
        <el-select
          v-model="form.dataScope"
          class="full-select"
          clearable
          placeholder="请选择数据范围"
        >
          <el-option
            v-for="item in dataScopeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { addRole, editRole } from '@/api/role'

  export default {
    name: 'RoleEdit',
    data() {
      const validateEnable = (rule, value, callback) => {
        if (value !== 0 && value !== 1) {
          callback(new Error('请选择状态'))
        } else {
          callback()
        }
      }
      return {
        dataScopeList: [
          { label: '全部', value: 1 },
          { label: '本部门', value: 2 },
          { label: '本部门及以下', value: 3 },
          { label: '个人创建', value: 4 },
        ],
        form: {
          name: '',
          code: '',
          roleDesc: '',
          isEnable: 1,
          dataScope: '',
        },
        rules: {
          name: [
            { required: true, message: '请输入角色名称', trigger: 'blur' },
          ],
          code: [{ required: true, message: '请输入编码', trigger: 'blur' }],
          roleDesc: [
            { required: true, message: '请输入描述', trigger: 'blur' },
          ],
          isEnable: [{ validator: validateEnable, trigger: 'blur' }],
          dataScope: [
            { required: true, message: '请选择数据范围', trigger: 'change' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        isSaving: false,
      }
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加角色'
        } else {
          this.title = '编辑角色'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            try {
              this.isSaving = true
              let res
              if (this.form.id) {
                res = await editRole(this.form)
              } else {
                res = await addRole(this.form)
              }
              console.log(this.form)
              let successMsg = '添加成功'
              let errorMsg = '添加失败'
              if (this.form.id) {
                successMsg = '修改成功'
                errorMsg = '修改成功'
              }
              if (res && res.status === 0) {
                this.$baseMessage(
                  res.msg || successMsg,
                  'success',
                  'vab-hey-message-success'
                )
                this.$emit('fetch-data')
                this.close()
              } else {
                this.$baseMessage(
                  res ? res.msg : errorMsg,
                  'error',
                  'vab-hey-message-error'
                )
              }
            } catch (e) {
              console.log(e)
              this.$baseMessage(
                e ? e.message : '操作失败',
                'error',
                'vab-hey-message-error'
              )
            }
            this.isSaving = false
          }
        })
      },
    },
  }
</script>
